<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="01_div和span.html" method="get" onsubmit="return f()">
    用户名:<input type="text" name="username" id="usernameId"><br>
    <input type="submit" value="提交按钮"><br>
</form>

<script>
   function f() {
       var usernameObj = document.getElementById("usernameId");
       // alert(usernameObj.value)
       if (usernameObj.value==''||usernameObj.value==null) {
            alert("用户名不能为空");
            return false;//阻止表单提交
       }
   }
</script>
</body>
</html>